﻿using System;
using System.ComponentModel.Composition;
using System.IO;
using System.Net;
using System.Text;
using System.Windows.Controls;
using FileRenamer;
using System.Windows;

namespace tvplugin
{
	[Export(typeof(ICommand))]
	public class TVPlugin : ICommand
	{
		private string showName = "";
		private Grid content;

		public string Name
		{
			get { return "TV Renamer"; }
		}

		public string Description
		{
			get { return "Connect to database and name use tv episodes proper name."; }
		}

		public string CommandName
		{
			get { return "test"; }
		}

		public string Convert(string input, string[] args)
		{
			return input + "01";
		}

		public string LoadXaml(ref Grid container)
		{
			StackPanel sp = new StackPanel();
			TextBox txtShowName = new TextBox();
			txtShowName.Text = "The It Crowd";
			txtShowName.Width = 200;
			Button btnSend = new Button();
			btnSend.Width = 200;
			btnSend.Content = "OK";
			btnSend.Click += new RoutedEventHandler(btnSend_Click);
			sp.Children.Add(txtShowName);
			sp.Children.Add(btnSend);
			container.Children.Add(sp);
			this.content = container;
			return "TV Namer";
		}

		private void btnSend_Click(object sender, RoutedEventArgs e)
		{
			TextBox txt = (TextBox)((StackPanel)content.Children[0]).Children[0];
			showName = txt.Text.Replace(" ", "%20");
			String input = getWebText(@"http://en.wikipedia.org/wiki/Special:Search?search=List%20of%20" + showName + @"%20episodes&go=Go");
			

		}

		private String getWebText(String url)
		{
			WikiTools.Access.AccessBrowser browser = new WikiTools.Access.AccessBrowser(new WikiTools.Access.Wiki(url));

			return browser.PageText;
			//StringBuilder sb = new StringBuilder();
			//byte[] buf = new byte[8192];
			//HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
			//HttpWebResponse response = (HttpWebResponse)request.GetResponse();
			//Stream resStream = response.GetResponseStream();
			//string temp = null;
			//int count = 0;
			//do
			//{
			//  count = resStream.Read(buf, 0, buf.Length);
			//  if (count != 0)
			//  {
			//    temp = Encoding.ASCII.GetString(buf, 0, count);
			//    sb.Append(temp);
			//  }
			//}
			//while (count > 0);

			//return sb.ToString();
		}
	}
}
